package background;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import shared.HTMLlist;

class Searcher1 {
 
    /**
	 * Returns a linked list created from a text file. The objects in this linked list has two elements: a string and a pointer 
	 * @param filename - the name of the text file
	 * @return HTMLlist start (the head of the linked list)
	 * @throws IOException
	 */
    public static HTMLlist readHtmlList (String filename) throws IOException {
        String name;
        HTMLlist start, current, tmp;

        // Open the file given as argument
        BufferedReader infile = new BufferedReader(new FileReader(filename));

        name = infile.readLine(); //Read the first line
        start = new HTMLlist (name, null);
        current = start;
        name = infile.readLine(); // Read the next line
        while (name != null) {    // Exit if there is none
            tmp = new HTMLlist(name,null);
            current.next = tmp;
            current = tmp;            // Update the linked list
            name = infile.readLine(); // Read the next line
        }
        infile.close(); // Close the file
        
        return start;
    }
    
    /**
     * Searches for a word in a linked list (HTMLlist)  
     * @param l - head of HTMLlist to check 
     * @param word - String with word to search for
     * @return a boolean value stating whether or not the word was found in the linked list
     */
    public static boolean exists (HTMLlist l, String word) {
        while (l != null) {
            if (l.str.equals (word)) {
                return true;
            }
            l = l.next;
        }
        return false;
    }
    
}